﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using Innvoice.Logica.GestorDB;
using Innvoice.Logica.GestorObject.Articulo;
using System.ComponentModel;

namespace Innvoice.Ws.Articulos.SubArticulos
{
    /// <summary>
    /// Lógica de interacción para ModificarCategoria.xaml
    /// </summary>
    public partial class ModificarSubArticulo : Window
    {
        #region Property

        private SubArticulo _subArticulo;
        private ArticuloDB articuloDb = new ArticuloDB();

        #endregion

        #region Contructor

        public ModificarSubArticulo()
        {
            InitializeComponent();
        }

        public ModificarSubArticulo(SubArticulo subArticulo)
        {
            InitializeComponent();
            _subArticulo = subArticulo;
            _subArticulo.PropertyChanged += new PropertyChangedEventHandler(subArticulo_PropertyChanged);
            DataContext = _subArticulo;
            TextDescripcion.Focus();
        }

        #endregion

        #region Windows Methods

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            GuardarElemento();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<Articulo> articulos = new List<Articulo>();

            articulos = articuloDb.ObtenerArticulos(-1);

            articuloComboBox.Items.Clear();
            articuloComboBox.ItemsSource = articulos;
            articuloComboBox.SelectedIndex = _subArticulo.CodigoArticulo - 1;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key.ToString().ToUpper())
            {
                case "F1":
                    GuardarElemento();
                    break;
                case "ESCAPE":
                    this.Close();
                    break;
                default:
                    return;
            }
        }

        private void subArticulo_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            btnGuardar.IsEnabled = _subArticulo.IsValid();
        }

        #endregion

        #region Auxiliar Methods

        private void GuardarElemento()
        {
            articuloDb.UpdateSubArticulo(_subArticulo, true);
            this.Close();
        }

        #endregion


    }
}
